package com.zhuo.schoolproject.controller;

import cn.dev33.satoken.util.SaResult;
import com.zhuo.schoolproject.entitys.Book;
import com.zhuo.schoolproject.service.BookService; // 需要导入BookService
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/book") // 建议添加基础路径
public class BookController {

    @Autowired
    private BookService bookService; // 依赖注入

    @PostMapping("/saveBook")
    public SaResult saveBook(@RequestPart("file") MultipartFile[] files, @RequestPart("book") Book book) {
        return bookService.saveBook(files, book);
    }

    /**
     * 查询书籍内容
     * @param id
     * @return
     */
    @GetMapping("id")
    public SaResult getBookById(@RequestParam Long id){
        return bookService.getBookById(id);
    }

    /**
     * 保存书籍内容
     * @param file
     * @param bookId
     * @return
     */
    @PostMapping("saveBookContent")
    public SaResult saveBookContent(@RequestPart MultipartFile file,@RequestPart Long bookId){
        return bookService.saveBookContent(file,bookId);
    }

    /**
     * 修改书籍
     * @param book
     * @return
     */
    @PostMapping("/updataBook")
    public SaResult updateBook(@RequestPart(value = "file",required = false) MultipartFile[] files,@RequestPart Book book){
        return bookService.updateBook(files,book);
    }

    /**
     * 修改书籍内容
     */
    @PostMapping("/updateBookContent")
    public SaResult updateBookContent(@RequestPart MultipartFile file, @RequestParam Long bookId){
        return bookService.updateBookContent(file, bookId);
    }

    /**
     * 删除书籍
     * @param id
     * @return
     */
    @GetMapping("deleteBook")
    public SaResult deleteBook(@RequestParam Long id){
        return bookService.deleteBook(id);
    }

    /**
     * 根据书籍ID查询书籍内容
     * @param id
     * @return
     */
    @GetMapping("fileByBookId")
    public SaResult fileByBookId(@RequestParam Long id){
        return bookService.fileByBookId(id);
    }

    /**
     * 分页查询书籍列表
     */
    @GetMapping("/page")
    public SaResult getBookPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String author,
            @RequestParam(required = false) Integer type) {
        return bookService.getBookPage(pageNum, pageSize, title, author, type);
    }
}